<!--待请款"-->
<template>
  <div>
    <el-form :model="checkForm" label-width="100px" class="demo-ruleForm" ref="checkForm">
      <el-form-item label="办理结果" prop="approveStatus"  :rules="[{ required: true, message: '请选择办理结果', trigger: 'change' }]">
        <el-radio-group v-model="checkForm.approveStatus" @change="handleChange">
          <el-radio label="1" class="green">补充资料</el-radio>
          <el-radio label="3" class="red">退回</el-radio><!--此处退件改为退回，退回到办押岗，状态值还是沿用退件的，但后台实际走的是退回逻辑-->
          <el-radio label="7" class="red">转派</el-radio>
          <el-radio label="9" class="red">通过</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="转派" prop="checkerId" v-if="checkForm.approveStatus === '7'" required>
        <el-select v-model="checkForm.checkerId" placeholder="请选择" style="width: 300px">
          <el-option v-for="item in sendCheckPersons" :key="item.id" :label="item.nameAndMobile" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="补充资料" prop="checkerId" v-if="checkForm.approveStatus === '1'" required>
        <el-select v-model="checkForm.checkerId" placeholder="请选择" style="width: 300px">
          <el-option v-for="item in supplyUser" :key="item.id" :label="item.displayName+'-'+item.roleName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="放款时间" prop="approvalDTO.loanTime" v-if="checkForm.approveStatus == 9" 
        :rules="[{ required: true, message: '放款时间不能为空', trigger: 'blur' }]">
          <el-date-picker  :picker-options="pickerOption"  v-model="checkForm.approvalDTO.loanTime" type="datetime" placeholder="选择日期" style="width: 300px" value-format="yyyy-MM-dd HH:mm:ss"> </el-date-picker>
      </el-form-item>

      <el-form-item label="反馈信息" prop="opinion" v-if="checkForm.approveStatus !== '9'">
           <el-input type="textarea" :rows="7" v-model="checkForm.opinion" style="width: 300px"></el-input>
      </el-form-item>

      <el-form-item
        label="放款金额"
        prop="loanAmount"
        v-if="checkForm.approveStatus === '9'"
        required
        :rules="[{ required: true, message: '放款金额不能为空' }]"
      >
        <el-input type="text" :rows="7" v-model="checkForm.loanAmount" style="width: 300px"></el-input>
        <span style="color:red">(单位：万)</span>
      </el-form-item>

      <el-form-item
        label="借款期限"
        prop="loanTerm"
        v-if="checkForm.approveStatus === '9'"
        style="width: 400px"
        required
        :rules="[{ required: true, message: '借款期限不能为空' }]"
      >
        <DicSelect dictType = "申请期限" :dictValue="checkForm.loanTerm" v-on:dictValue="checkForm.loanTerm=$event" style="width: 100px"/>
      </el-form-item>


      <el-form-item
        label="还款方式"
        prop="repayMethod"
        v-if="checkForm.approveStatus === '9'"
        style="width: 400px"
        :rules="{ required: true, message: '还款方式不能为空',trigger: 'change'}"
      >
        <DicSelect
          dictType="还款方式"
          :dictValue="checkForm.repayMethod"
          v-on:dictValue="checkForm.repayMethod = $event"
          style="width: 100px"
        />
      </el-form-item>
      <el-form-item
        label="还款金额"
        prop="loanMoney"
        v-if="checkForm.approveStatus === '9'"
        required
        :rules="[{ required: true, message: '还款金额不能为空', trigger: 'blur' }]"
      >
        <el-input type="text" :rows="7" v-model="checkForm.loanMoney" style="width: 300px"></el-input>
        <span style="color:red">(单位：万)</span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('checkForm')" v-loading="loading">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { loanCheck, listRedePloyUser, getTaskCandiates } from '@/api/project';
import DicSelect from '@/components/select/index';
export default {
  components: { DicSelect },
  props: {
     projectId: undefined,
     taskId: undefined
  },
  data() {
    return {
      pickerOption: {
        disabledDate(current) {
          return current && current.getTime() > new Date().getTime();
        },
      },
      checkForm: {
        approveStatus: undefined,
        checkerId: undefined,
        loanAmount: undefined,
        loanTerm: undefined,
        loanMoney: undefined,
        repayMethod: undefined,
        projectId: undefined,
        opinion: undefined,
        approvalDTO:{
          loanTime:undefined
        }
      },
      sendCheckPersons: [],
      supplyUser: [],
      loading: false
    };
  },
  created() {
    // 初始化数据
    this.listRedePloyUser();
    this.supplyUsers();
  },
  methods: {
    submitForm(checkForm) {
      this.$refs[checkForm].validate((valid) => {
          if (valid) {
            this.checkForm.projectId = this.projectId;
            this.checkForm.taskId = this.taskId;
            this.checkForm.approvalDTO.projectId = this.projectId
            if (this.checkForm.loanAmount >= 5000) {
              this.$message.error('放款金额不能超过5000万');
              return;
            }
            if (this.checkForm.loanMoney >= 5000) {
              this.$message.error('还款金额不能超过5000万');
              return;
            }
            this.$confirm('你确定要提交吗?', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',}).then(() => {
              // 开始提交信息
              this.loading = true;
              loanCheck(this.checkForm).then((data) => {
                this.loading = false;
               this.$notify.success({
                title: '成功',
                message: '操作成功'
              })
                //跳转至贷后管理列表
                this.$router.push({path: '/task/task_list'})
              }).catch(() => {
                this.loading = false;
              })
            });
          } else {
            return false;
          }
        });
    },
    listRedePloyUser() {
      // 查询数据 构造页面
      listRedePloyUser('请款管理岗').then((data) => {
        this.sendCheckPersons = data.data;
        console.log('响应数据:%o', data);
      });
    },
    supplyUsers() {
      // 查询数据 构造页面
      getTaskCandiates(this.taskId).then((data) => {
        this.supplyUser = data.data;
        console.log('响应数据:%o', data);
      });
    },
    //节点判断
    handleChange(val) {
      this.restForm();
      this.$nextTick(() => {
        this.$refs['checkForm'].clearValidate()
      })
      this.checkForm.approveStatus = val;
      return;
    },
    restForm() {
      this.checkForm = {
        approveStatus: undefined,
        checkerId: undefined,
        opinion: undefined,
        loanAmount: undefined,
        loanTerm: undefined,
        loanMoney: undefined,
        repayMethod: undefined,
        approvalDTO:{
          loanTime:undefined
        }
      };
    },
  },
};
</script>
